/**
 * This code was created by David Knape (http://bumpslide.com/) 
 * Released under the open-source MIT license.
 * 
 * Copyright (c) 2008 by Bumpslide, Inc. and contributing authors
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.   
 */
 package pdf417lib.flash {
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import pdf417lib.Pdf417lib;		

	/**
	 * PDF 417 Barcode Test App
	 * 
	 * This is a barebones (non-flex) demonstration of how to use
	 * the pdf417lib barcode generator.  You can compile this with 
	 * mxmlc using something like the command below, or you can setup 
	 * a fla with this as the document class.
	 * 
	 * @mxmlc -sp+=../../ -default-background-color=#eeeeee
	 * @author David Knape
	 */
	public class BarcodeTestApp extends Sprite {
		
		// children
		public var barcode:Shape;
		public var displayText:TextField;
		public var statusText:TextField;
		
		/**
		 * Wait for stage before initializing
		 */
		function BarcodeTestApp() {			
			addEventListener(Event.ADDED_TO_STAGE, init);	
		}

		/**
		 * Initialize stage scale mode and add children
		 */
		protected function init(e:Event):void {			
			// init stage
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			// create children
			displayText = getTextField( 400, 0, true );
			statusText = getTextField( 400, 0x880000 );
			displayText.x = PADDING;
			displayText.y = PADDING;		
			addChild( displayText );	
			addChild( statusText );
			
			// lorem ipsum text
			displayText.text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer lobortis volutpat lectus. Maecenas tristique adipiscing sapien. Vestibulum ullamcorper interdum velit. Duis ornare tincidunt nunc. Aliquam nisi leo, vestibulum non, consectetuer et, feugiat a, dui. Curabitur aliquet. Donec lectus felis, bibendum vitae, viverra sit amet, sollicitudin sed, mauris. Fusce consectetuer augue sit amet urna. Integer odio. Cras dui velit, rhoncus et, luctus vitae, pharetra at, orci. Etiam ornare felis sit amet sem. Curabitur condimentum quam in quam. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus eget eros. Donec ut neque nec mauris tincidunt pharetra. Ut posuere blandit dui. Nullam euismod. Mauris in velit non enim scelerisque scelerisque. Nulla non nisi eget ipsum accumsan placerat.";			
			
			// redraw when text is changed
			displayText.addEventListener(Event.CHANGE, redraw);
			
			// kickstart
			redraw();
		}

		protected function redraw(event:Event = null):void {	
			
			// figure out where we are we going?
			var ypos:Number = displayText.getBounds(this).bottom + PADDING;
			
			// reset status text
			statusText.text = "";
			statusText.y = ypos;
			
			// remove old barcode
			if(barcode!=null && contains(barcode)) {
				removeChild(barcode);	
			}	
			
			// generate new barcode
			try {				
				barcode = Pdf417lib.generateBarcode(displayText.text);	
				barcode.x = PADDING;
				barcode.y = ypos;
				barcode.scaleX = 1;
				barcode.scaleY = 3;
				addChild(barcode);			
			} catch (e:Error) {
				statusText.text = e.message;
			}	
		}
		
		
		//----------------------------------
		//  PRIVATE (helpers and constants)
		//----------------------------------
		
		private const PADDING:uint = 20;
		
		private function getTextField( width:Number, color:Number=0, isInputField:Boolean=false ) : TextField {
			var t:TextField = new TextField();
			t.multiline = t.wordWrap = true; 
			if(isInputField) {
				t.border = true;
				t.type = TextFieldType.INPUT;
				t.background = true;
				t.backgroundColor = 0xeeeeee;
			}
			t.textColor=color;
			t.width = width;	
			t.type = TextFieldType.INPUT;
			t.autoSize = TextFieldAutoSize.LEFT;		
			t.defaultTextFormat = new TextFormat('_sans', 13);
			return t;
		}
	}
}
